<?php 

	include_once('../sys/sys.php');
	header('Content-Type: text/html; charset=utf-8');
	
	
	
	//用户ID
	$uid = 0;
	
	
	$tid = $_GET['tid'];
	
	if ($_GET['act'] == "del"){
		if(!is_numeric($tid) || $tid < 0){
			show_normal_mesR('TAG ID错误','TAG ID错误','TAG查询');
		}
		del_fav($uid, $tid);
		exit;
	}
	
	
	
	
	
	//显示收藏夹
	if(is_null($tid)){
		get_fav_list($uid);
		exit;
	}
	
	if(!is_numeric($tid) || $tid < 0){
		show_normal_mesR('TAG ID错误','TAG ID错误','TAG查询');
	}
	
	
	$where = 'AND tid= ' . $tid . ' AND uid= ' . $uid . '';
	$result = $DB->build_and_exec_query(
			array('select' => 'COUNT(*) as max','from'   => 'hhfav','where'	=> "1 " . $where ,)
	);
	//当已经存在的时候更新日期
	if ($result['max'] > 0) {
			
	}
	//当不存在时插入
	else{
		do_favinsert($tid,$uid);
	}
	

	function do_favinsert($tid,$uid){
		global $DB;
		$DB->do_insert( 'hhfav', array( 'tid'			=> $tid,
				'uid'				=> $uid,
		)		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	ECHO '添加成功，1秒后返回';
	echo '<meta http-equiv=refresh content="1;URL=' . $_SERVER['HTTP_REFERER'] . '">';exit;
	echo "<script language='JavaScript'>javascript:history.back(-1);</script>";
	
	function get_fav_list($uid){
		global $DB;
		global $Elsesky,$sysinfo;
		//获取分类以及分页号
		$st = $_GET['st'];
		if(is_null($st)){
			$st = 0;
		}
		//列出的图片数量(注意,不要超过15张)
		$listnum = 19;
		
		$DB->build_query(array(
				'select' 	=> 'tid',
				'from'		=> 'hhfav',
				'where' 	=> 'uid=' . $uid,
				'limit'		=> array($st,$listnum)
		));
		$qid = $DB->exec_query();
		while ($row = $DB->fetch_row($qid)) {
			$tid_array .= $row['tid'] . ',' ;
		}
		if (strlen($tid_array) < 1){
			$tid_array = '0,';
		}
		//处理掉逗号
		$tid_array = substr($tid_array,0,strlen($tid_array) - 1);
		
		//echo $tid_array;exit;
		
		$array_piclist = array();
		
		
		$DB->build_query(array(
				'select' 	=> 'id,name,lastupdate,count',
				'from'		=> 'hhtags',
				'where' 	=> 'id in(' . $tid_array . ')',
				'order'		=> 'lastupdate DESC',
				'limit'		=> array($st,$listnum)
		));
		$qid = $DB->exec_query();
		$i = 0;
		while ($row = $DB->fetch_row($qid)) {
			if($row['lastupdate'] <= 0) $row['lastupdate'] = 0;
			$array_piclist[$i]['tagname'] = $row['name'];
			$array_piclist[$i]['id'] = $row['id'];
			$array_piclist[$i]['count'] = $row['count'];
			$array_piclist[$i]['lastupdate'] = get_date_time($row['lastupdate']);
			$array_piclist[$i]['downloadpicslink'] = SITE_URL . 'hh/pics.php?action=getpicfiles&tid=' .$row['id'];
			$array_piclist[$i]['picslink'] = SITE_URL . 'hh/pics.php?action=getpicurls&tid=' .$row['id'];
			$array_piclist[$i]['getnewlink'] = SITE_URL . 'hh/cr.php?tid=' .$row['id'];
			$array_piclist[$i]['favlink'] = SITE_URL . 'hh/fav.php?act=del&tid=' . $row['id'];
			$array_piclist[$i]['clearlink'] = SITE_URL . 'hh/clear.php?tid=' . $row['id'];
			$i++;
		}
		//处理分页信息
		//获取总TAG数
		$totalnumbers = get_count_of_field('hhfav','uid=' . $uid);
		//$totalnumbers = get_count_of_field('hhfav','');
		$pagelink = $Elsesky->PageLinkR($st,$totalnumbers,$listnum,SITE_URL."hh/fav.php?&st=<:st:>");
		
		//处理最常访问内容
		$ccount = 9;
		$array_ccount = array();
		
		$DB->build_query(array(
				'select' 	=> '*',
				'from'		=> 'hhtags',
				'order'		=> 'scount DESC',
				'limit'		=> array(0,$ccount)
		));
		$qid = $DB->exec_query();
		$i = 0;
		while ($row = $DB->fetch_row($qid)) {
			$array_ccount[$i]['tidname'] = $row['name'];
			$array_ccount[$i]['ccount'] = $row['scount'];
			$array_ccount[$i]['tidlink'] = SITE_URL . 'nonadm/search.php?name=' . $row['name'];
			$i++;
		}
		
		
		$page = init_smart_template_obj('hh/list.html',CACHE_DIR,CACHE_DIR,TEMPLATE_DIR);
		$page->assign("piclist",$array_piclist);
		$page->assign("system",$sysinfo);
		$page->assign("pagelink",$pagelink);
		$page->assign("searchpg",SITE_URL . 'hh/search.php');
		$page->assign("title",'收藏');
		$page->assign("tidlink",$array_ccount);
		$page->assign("ccount",$ccount);
		$page->output();
		
	}
	
	function del_fav($uid,$tid){
		global $DB;
		global $Elsesky,$sysinfo;
		$where = 'AND tid= ' . $tid . ' AND uid= ' . $uid . '';
		$result = $DB->build_and_exec_query(
				array('select' => 'COUNT(*) as max','from'   => 'hhfav','where'	=> "1 " . $where ,)
		);
		if ($result['max'] > 0) {
			$DB->simple_delete('hhfav', "1 " . $where );
			$DB->exec_query();
			ECHO '删除成功，1秒后返回';
			echo '<meta http-equiv=refresh content="1;URL=' . $_SERVER['HTTP_REFERER'] . '">';
		}
	}
	
	
	
?>